/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.miit.elearning.vtutor.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ru.miit.elearning.vtutor.check.object.CheckObjectTypeHelper;
import ru.miit.elearning.vtutor.check.ZOSCheckServiceClient;
import ru.miit.elearning.vtutor.check.object.AbstractCheckObject;
import ru.miit.elearning.vtutor.data.CheckObject;
import ru.miit.elearning.vtutor.data.DataMiner;
import ru.miit.elearning.vtutor.ftp.FTPClientDecorator;

/**
 *
 * @author Cyclone
 */
public class FtpInspectServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        DataMiner miner = null;
        FTPClientDecorator ftp = null;
        try {

            //Integer labId = Integer.parseInt(request.getParameter("labId"));
            int courseId = Integer.parseInt(request.getParameter("courseId"));
            String path = request.getParameter("path");
            int type = Integer.parseInt(request.getParameter("type"));

            miner = new DataMiner();

            String ftpFullUrl = ZOSCheckServiceClient.getZosFtpServerHostByCourse(courseId);

            ftp = new FTPClientDecorator();
            ftp.connect(ftpFullUrl);
            if(type == CheckObjectTypeHelper.CHECH_OBJECT_TYPE_PDS) {
                int dot = path.indexOf(".");
                ftp.cd(path.substring(0, dot));
                path = path.substring(dot+1);
            }
            
            CheckObject co = new CheckObject();
            co.setType(type);
            co.setPath(path);
            AbstractCheckObject aco = CheckObjectTypeHelper.getCheckableInstance(co);

            String contentText = aco.getTextContent(ftp);
            ftp.close();

            out.print(contentText);
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(FtpInspectServlet.class.getName()).log(Level.SEVERE, null, ex);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            if (ftp != null) {
                try {
                    ftp.close();
                } catch (Exception e) {
                    System.err.println("Failed to close FTP connection with message: " + e.getMessage());
                }
            }
            if (miner != null) {
                miner.close();
            }

            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
